import pathlib
import shutil

import keras_autodoc

PAGES = {
    'image_classifier.md': [
        'autokeras.ImageClassifier',
        'autokeras.ImageClassifier.fit',
        'autokeras.ImageClassifier.predict',
        'autokeras.ImageClassifier.evaluate',
    ],
    'image_regressor.md': [
        'autokeras.ImageRegressor',
        'autokeras.ImageRegressor.fit',
        'autokeras.ImageRegressor.predict',
        'autokeras.ImageRegressor.evaluate',
    ],
    'text_classifier.md': [
        'autokeras.TextClassifier',
        'autokeras.TextClassifier.fit',
        'autokeras.TextClassifier.predict',
        'autokeras.TextClassifier.evaluate',
    ],
    'text_regressor.md': [
        'autokeras.TextRegressor',
        'autokeras.TextRegressor.fit',
        'autokeras.TextRegressor.predict',
        'autokeras.TextRegressor.evaluate',
    ],
    'structured_data_classifier.md': [
        'autokeras.StructuredDataClassifier',
        'autokeras.StructuredDataClassifier.fit',
        'autokeras.StructuredDataClassifier.predict',
        'autokeras.StructuredDataClassifier.evaluate',
    ],
    'structured_data_regressor.md': [
        'autokeras.StructuredDataRegressor',
        'autokeras.StructuredDataRegressor.fit',
        'autokeras.StructuredDataRegressor.predict',
        'autokeras.StructuredDataRegressor.evaluate',
    ],
    'auto_model.md': [
        'autokeras.AutoModel',
        'autokeras.AutoModel.fit',
        'autokeras.AutoModel.predict',
        'autokeras.AutoModel.evaluate',
    ],
    'base.md': [
        'autokeras.Node',
        'autokeras.Preprocessor',
        'autokeras.Block',
        'autokeras.Block.build',
        'autokeras.Head',
    ],
    'node.md': [
        'autokeras.ImageInput',
        'autokeras.Input',
        'autokeras.StructuredDataInput',
        'autokeras.TextInput',
    ],
    'block.md': [
        'autokeras.ConvBlock',
        'autokeras.DenseBlock',
        'autokeras.EmbeddingBlock',
        'autokeras.Merge',
        'autokeras.ResNetBlock',
        'autokeras.RNNBlock',
        'autokeras.SpatialReduction',
        'autokeras.TemporalReduction',
        'autokeras.XceptionBlock',
        'autokeras.ImageBlock',
        'autokeras.StructuredDataBlock',
        'autokeras.TextBlock',
    ],
    'preprocessor.md': [
        'autokeras.FeatureEngineering',
        'autokeras.ImageAugmentation',
        'autokeras.LightGBM',
        'autokeras.Normalization',
        'autokeras.TextToIntSequence',
        'autokeras.TextToNgramVector',
    ],
    'head.md': [
        'autokeras.ClassificationHead',
        'autokeras.RegressionHead',
    ],
}


ROOT = 'http://autokeras.com/'

autokeras_dir = pathlib.Path(__file__).resolve().parents[1]


def generate(dest_dir):
    template_dir = autokeras_dir / 'docs' / 'templates'
    doc_generator = keras_autodoc.DocumentationGenerator(
        PAGES,
        'https://github.com/keras-team/autokeras/blob/master',
        template_dir,
        autokeras_dir / 'examples',
    )
    doc_generator.generate(dest_dir)
    readme = (autokeras_dir / 'README.md').read_text()
    index = (template_dir / 'index.md').read_text()
    index = index.replace('{{autogenerated}}', readme[readme.find('##'):])
    (dest_dir / 'index.md').write_text(index, encoding='utf-8')
    shutil.copyfile(autokeras_dir / '.github' / 'CONTRIBUTING.md',
                    dest_dir / 'contributing.md')


if __name__ == '__main__':
    generate(autokeras_dir / 'docs' / 'sources')
